package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;
import com.wwwc.index.web.ejb.*;

public class ShoppingHotDeals extends HttpServlet {
    private EJBDatabaseQuery ejbdb;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {	
	try {
	    response.setContentType("text/html");
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();
	    String  user_ip      = request.getRemoteAddr();
	    String call_path  = request.getContextPath()+request.getServletPath();
	    String mid        = (String)request.getAttribute("mid");
	    String sub_dir    = request.getParameter("sub_dir");
	    String table_name = request.getParameter("table_name");

	    if(mid == null || sub_dir==null || table_name == null) {
		System.out.println("ShoppingHotDeals:Error:101");
		return;
	    }

	    String max_rows  = request.getParameter("max_rows");
	    if(max_rows == null) {
		max_rows = "10";
	    }

	    String user_name  = null;
	    int user_age      = 0;
	    int user_level    = 0;
	    String user_fname   = null;
	    String user_since   = null;
	    String user_edu     = null;
	    String user_zip     = null;
	    String user_sex     = null;
	    String screen_w     = null;
	    String screen_h     = null;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		System.out.println("ContentDisplayServlet:Error:104");
		return;
	    }

	    user_name   = (String)user_info.get("user_name");
	    user_age    = Integer.parseInt((String)user_info.get("user_age"));
	    user_level  = Integer.parseInt((String)user_info.get("user_level"));
	    user_fname  = (String)user_info.get("user_fname");
	    user_since  = (String)user_info.get("user_since");
	    user_edu    = (String)user_info.get("user_edu");
	    user_zip    = (String)user_info.get("user_zip");
	    user_sex    = (String)user_info.get("user_sex");
	    screen_w    = (String)user_info.get("screen_w");
	    screen_h    = (String)user_info.get("screen_h");

	    //int sw = Integer.parseInt(screen_w);
	    //int sh = Integer.parseInt(screen_h);

	    Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	    if(dir_hs == null) {
		System.out.println("ShoppingHotDeals:Error:103");
		return;
	    }

	    String sid = (String)dir_hs.get(mid+"_sid");
	    if(sid == null) {
		System.out.println("ShoppingHotDeals:Error:104");
		return;
	    }

	    String aid =  request.getParameter("aid");	   
	    String pid =  request.getParameter("pid");

	    if(pid == null || !MyStringCheck.isNumber(pid)) {
		pid = "1";
	    }

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = null;

	    int row_counter = ejbdb.getRowCounter(table_name);
	    if(row_counter == -1) {
		return;
	    }

	    //===============================================================
	    // access control
	    //===============================================================

	    Hashtable achs = (Hashtable)request.getAttribute("achs");
	    if(achs == null) {
		return;
	    }

	    boolean manager  = (Boolean.valueOf((String)achs.get("show_manager"))).booleanValue();
	    boolean admin    = (Boolean.valueOf((String)achs.get("show_admin"))).booleanValue();

	    StringBuffer sbf = new StringBuffer();
	    sbf.append(MyHtml.pageCounter(row_counter, max_rows, pid, call_path+"?mid="+mid));
	    sbf.append((new ServletBean()).getSubdirInfo(request));
	    ShoppingBean sbean = new ShoppingBean();

	    //=================================================================
	    // hot deals list
	    //=================================================================
	    
	    if(aid == null || aid.equals("0")) {
		StringBuffer add_bar = new StringBuffer();
		add_bar = MyHtml.makeLinkButton("Add", call_path+"?mid="+mid+"&aid=2-1");
		if(user_level >0) {
		    sbf.append(MyHtml.tableWraper(1, "center", add_bar));
		}
		sbf.append(sbean.listHotDeals(request, mid, table_name, max_rows, pid, manager, admin));
		out.println(sbf);
		return;
	    }

	    //=================================================================
	    // open a new window to linked url and update counter
	    //=================================================================

	    if(aid != null && aid.equals("1-1")) {
		String wid  = request.getParameter("wid");
		String furl = request.getParameter("url");

		out.println(MyHtml.replaceTopUrl(furl));
		sbean.updateHotDealsCounter(wid, table_name);
	    }
	   
	    //==================================================================
	    // add new hot detal
	    //==================================================================

	    if(aid != null && aid.equals("2-1")) {
		out.println(MyJavaScript.disableHistory());
		out.println(sbean.addNewHotDeal(request, response, session, mid, "2-1", "0"));
	    }

	    //==================================================================
	    // edit
	    //==================================================================

	    if(aid != null && aid.equals("3-1")) {
		out.println(MyJavaScript.disableHistory());
		out.println(sbean.editHotDeal(request, response, session, mid, "3-1", "0"));
	    }

	    //==================================================================
	    // delete
	    //==================================================================

	    if(aid != null && aid.equals("4-1")) {
		out.println(MyJavaScript.disableHistory());
		sbean.deleteHotDeal(request, response, mid, "0");
	    }
	}
	catch(IOException e) {
	    System.out.println("ShoppingHotDeals:Error:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }
}
